package com.wing.web.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.exception.BusinessException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * JWT工具类
 *
 * @author
 */
@Slf4j
public class JwtUtils {

    @SneakyThrows
    public static JSONObject getJwtPayload() {
        String payload = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
        if (null == payload) {
            throw new BusinessException("请传入认证头");
        }
        JSONObject jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload,StandardCharsets.UTF_8.name()));
        return jsonObject;
    }

    /**
     * 解析JWT获取用户ID
     *
     * @return
     */
    public static Long getLoginUserId() {
        Long id = getJwtPayload().getLong(SecurityConstants.USER_ID_KEY);
        return id;
    }

    /**
     * 解析JWT获取获取用户名
     *
     * @return
     */
    public static String getLoginUsername() {
        String username = getJwtPayload().getStr(SecurityConstants.USER_NAME_KEY);
        String nickname = getJwtPayload().getStr(SecurityConstants.NICK_NAME_KEY);
//        if (username!=null&&username.equals(nickname)){
//            return username;
//        }
        return nickname+"|"+username;
    }

    public static String getUsername() {
        String username = getJwtPayload().getStr(SecurityConstants.USER_NAME_KEY);
//        if (username!=null&&username.equals(nickname)){
//            return username;
//        }
        return username;
    }

    public static String getLoginNickname() {
        String username = getJwtPayload().getStr(SecurityConstants.NICK_NAME_KEY);
        return username;
    }

    public static String getEnterpriseCode() {
        String enterpriseCode = getJwtPayload().getStr(SecurityConstants.ENTERPRISE_CODE_KEY);
        return enterpriseCode;
    }

    public static String getChannelCode() {
        String channelCode = getJwtPayload().getStr(SecurityConstants.CHANNEL_CODE_KEY);
        return channelCode;
    }

    /**
     * 查询所属企业
     * @return 企业 id
     */
    public static Long getEnterpriseId() {
        String enterpriseID = getJwtPayload().getStr(SecurityConstants.ENTERPRISE_ID_KEY);
        if (enterpriseID!=null){
            return Long.parseLong(enterpriseID);
        }
        return null;
    }

    public static Long getPostId() {
        String postId = getJwtPayload().getStr(SecurityConstants.POST_ID_KEY);
        if (postId!=null){
            return Long.parseLong(postId);
        }
        return null;
    }

    public static String getEnterpriseName() {
        String enterpriseName = getJwtPayload().getStr(SecurityConstants.ENTERPRISE_NAME_KEY);
        return enterpriseName;
    }
    public static String getCostCenter() {
        String costCenter = getJwtPayload().getStr(SecurityConstants.COST_CENTER);
        return costCenter;
    }
    public static Long getDepartmentId() {
        String departmentID = getJwtPayload().getStr(SecurityConstants.DEPARTMENT_ID_KEY);
        if (departmentID!=null){
            return Long.parseLong(departmentID);
        }
        return null;
    }

    public static Long getRoleId() {
        String roleId = getJwtPayload().getStr(SecurityConstants.ROLE_ID_KEY);
        if (roleId!=null){
            return Long.parseLong(roleId);
        }
        return null;
    }

    public static String getDepartmentName() {
        String departmentName = getJwtPayload().getStr(SecurityConstants.DEPARTMENT_NAME_KEY);
        return departmentName;
    }

    public static String getPhone() {
        String phone = getJwtPayload().getStr(SecurityConstants.USER_PHONE_KEY);
        return phone;
    }

    public static String getRoleName() {
        String roleName = getJwtPayload().getStr(SecurityConstants.ROLE_NAME_KEY);
        return roleName;
    }

    public static String getRoleCode() {
        String roleCode = getJwtPayload().getStr(SecurityConstants.ROLE_CODE_KEY);
        return roleCode;
    }

    public static String getCabAuthCodes() {
        String cabAuthCodes = getJwtPayload().getStr(SecurityConstants.CAB_AUTH_CODES_KEY);
        return cabAuthCodes;
    }
    public static String getClientId() {
        return getJwtPayload().getStr(SecurityConstants.JWT_CLIENT_ID);
    }
    /**
     * JWT获取用户角色列表
     *
     * @return 角色列表
     */
    public static List<String> getLoginRoles() {
        List<String> roles = null;
        JSONObject payload = getJwtPayload();
        if (payload.containsKey(SecurityConstants.JWT_AUTHORITIES_KEY)) {
            roles = payload.getJSONArray(SecurityConstants.JWT_AUTHORITIES_KEY).toList(String.class);
        }
        return roles;
    }
}
